package vn.ugame.model.dal;

import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import vn.ugame.exception.DataConnectionFailed;
import vn.ugame.exception.InvalidEntityClass;
import vn.ugame.model.entity.IntWapper;
import vn.ugame.model.entity.Order;

public class OrderDA {

    public Order findOrderByActivatedCode(String code) throws DataConnectionFailed,
            SQLException, InvalidEntityClass {
        Order order = null;
        List<Order> orders = findOrders(null, null, null, null, null, new Locale("en-US"), code, 
                null, null, null, null, null, null, null, null);
        if (orders.size() > 0) {
            order = orders.get(0);
        }
        return order;
    }
    
    public Order findOrderByIMEI(String imei) throws DataConnectionFailed,
            SQLException, InvalidEntityClass {
        Order order = null;
        List<Order> orders = findOrders(null, null, null, null, null, new Locale("en-US"), null, 
                null, imei, null, null, null, null, null, null);
        if (orders.size() > 0) {
            order = orders.get(0);
        }
        return order;
    }

    public List<Order> findOrders(
            String customerName, String customerId, String phoneNumber,
            String itemTag, String itemId, Locale locale,
            String activatedCode, java.util.Date activatedDate, String imei,
            Double price, String priceUnit, Double priceVND,
            Integer fromIndex, Integer toIndex, IntWapper count)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {

        List<Order> orderList = new ArrayList<Order>();

        Connection conn = ConnectionFactory.getInstance().createDBConnection();

        String query = "{ call findOrders(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }";

        CallableStatement cs = conn.prepareCall(query);
        if (customerName != null) {
            try {
                cs.setBytes(1, customerName.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException ex) {
            }
        } else {
            cs.setNull(1, Types.VARCHAR);
        }
        cs.setString(2, customerId);
        cs.setString(3, phoneNumber);
        if (itemTag != null) {
            try {
                cs.setBytes(4, itemTag.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException ex) {
            }
        } else {
            cs.setNull(4, Types.VARCHAR);
        }
        cs.setString(5, itemId);
        if (locale == null) {
            cs.setNull(6, Types.VARCHAR);
        } else {
            cs.setString(6, locale.getLanguage());
        }
        cs.setString(7, activatedCode);
        if (activatedDate == null) {
            cs.setNull(8, Types.DATE);
        } else {
            cs.setDate(8, new java.sql.Date(activatedDate.getTime()));
        }
        cs.setString(9, imei);
        if (price == null) {
            cs.setNull(10, Types.DOUBLE);
        } else {
            cs.setDouble(10, price.doubleValue());
        }
        cs.setString(11, priceUnit);
        if (priceVND == null) {
            cs.setNull(12, Types.DOUBLE);
        } else {
            cs.setDouble(12, priceVND.doubleValue());
        }
        if (fromIndex == null) {
            cs.setInt(13, 0);
        } else {
            cs.setInt(13, fromIndex.intValue());
        }
        if (toIndex == null) {
            cs.setInt(14, 0);
        } else {
            cs.setInt(14, toIndex.intValue());
        }
        cs.registerOutParameter(15, Types.INTEGER);

        ResultSet rs = cs.executeQuery();
        EntityFactory factory = EntityFactory.createEntityFactory();
        while (rs.next()) {
            Order o = factory.createEntity(rs, Order.class);
            orderList.add(o);
        }

        if (count != null) {
            count.setValue(cs.getInt(15));
        }

        rs.close();
        cs.close();
        conn.close();

        return orderList;
    }

    public void createOrder(String itemId, String customerId,
            java.util.Date orderDate, String activatedCode, double price,
            String priceUnit, double priceVND)
            throws DataConnectionFailed, SQLException {

        Connection conn = ConnectionFactory.getInstance().createDBConnection();

        String query = "{ call createOrder(?,?,?,?,?,?,?) }";
        CallableStatement cs = conn.prepareCall(query);
        cs.setString(1, itemId);
        cs.setString(2, customerId);
        cs.setTimestamp(3, new Timestamp(orderDate.getTime()));
        cs.setString(4, activatedCode);
        cs.setDouble(5, price);
        cs.setString(6, priceUnit);
        cs.setDouble(7, priceVND);

        cs.execute();

        cs.close();
        conn.close();
    }

    public void activatedItem(String activatedCode, String imei)
            throws DataConnectionFailed, SQLException {
        Connection conn = ConnectionFactory.getInstance().createDBConnection();

        String query = "{ call activateItem(?,?) }";
        CallableStatement cs = conn.prepareCall(query);
        cs.setString(1, activatedCode);
        cs.setString(2, imei);
        cs.execute();

        cs.close();
        conn.close();
    }
}
